﻿using System;
using System.Collections.Generic;
using System.Linq;
using SharpErp.Data.Common.Entity;
using SharpErp.Data.Common.View;

namespace SharpErp.Data.Service
{
	public partial class SharpErpServiceImpl : ISharpErpService
    {
		List<CustomerThirdClass> ISharpErpService.GetSysCustomerThirdClass()
		{
			using (var context = new Common.Context.SqliteDbContext ()) {
				var list = new System.Collections.Generic.List<CustomerThirdClass>(context.AllArray(context.CustomerThirdClasEntities));
                return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
            }
		}

		List<CustomerThirdClass> ISharpErpService.GetCustomerThirdClassByParentId(int parentId)
		{
	        using (var context = new Common.Context.SqliteDbContext())
	        {
	            return new List<CustomerThirdClass>( context.SelectAll(context.CustomerThirdClasEntities,
	                p => p.ParentId == parentId && (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
	        }
		}

        List<CustomerThirdClass> ISharpErpService.GetCustomerThirdClassByParentIds(HashSet<int> ids)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                return new List<CustomerThirdClass>(context.SelectAll(context.CustomerThirdClasEntities,
                    p => ids != null && ids.Contains(p.ParentId) && (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
            }
        }

        void ISharpErpService.AddCustomerThirdClass(CustomerThirdClass item)
		{
			using (var context = new Common.Context.SqliteDbContext ()) {
				context.Add (context.CustomerThirdClasEntities, item);
			}
		}
	
		void ISharpErpService.UpdateCustomerThirdClass(CustomerThirdClass item)
		{
			using (var context = new Common.Context.SqliteDbContext ()) {
				context.Update (context.CustomerThirdClasEntities, item);
			}
		}
	
		void ISharpErpService.DeleteCustomerThirdClass(int id)
		{
			using (var context = new Common.Context.SqliteDbContext ()) {
				context.Delete(context.CustomerThirdClasEntities, p=>p.Id == id);
			}
		}

		List<CustomerSecondClass> ISharpErpService.GetSysCustomerSecondClass()
		{
			using (var context = new Common.Context.SqliteDbContext ()) {
				var list = new System.Collections.Generic.List<CustomerSecondClass>(context.AllArray(context.CustomerSencodClasEntities));
                return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
            }
		}

	    public List<CustomerSecondClass> GetCustomerSecondClassByParentId(int parentId)
	    {
	        using (var context = new Common.Context.SqliteDbContext())
	        {
	            return new List<CustomerSecondClass>( context.SelectAll(context.CustomerSencodClasEntities,
	                p => p.ParentId == parentId && (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
	        }

	    }

        public List<CustomerSecondClass> GetCustomerSecondClassByParentIds(List<int> ids)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                return new List<CustomerSecondClass>(context.SelectAll(context.CustomerSencodClasEntities,
                    p => ids.Contains(p.ParentId) && (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
            }
        }



        int ISharpErpService.AddCustomerSecondClass(CustomerSecondClass item)
		{
			using (var context = new Common.Context.SqliteDbContext ()) {
				return context.Add (context.CustomerSencodClasEntities, item).Id;
			}
		}
	
		void ISharpErpService.UpdateCustomerSecondClass(CustomerSecondClass item)
		{
			using (var context = new Common.Context.SqliteDbContext ()) {
				context.Update (context.CustomerSencodClasEntities, item);
			}
		}
	
		void ISharpErpService.DeleteCustomerSecondClass(int id)
		{
			using (var context = new Common.Context.SqliteDbContext ()) {
				context.Delete(context.CustomerSencodClasEntities, p=>p.Id == id);
			}
		}

	    public string GetCustomerSecondClassNextCode()
	    {
	        using (var context = new Common.Context.SqliteDbContext())
	        {
	            var maxId = context.CustomerSencodClasEntities.Max(p => p.Code);
	            if (string.IsNullOrEmpty(maxId))
	            {
	                return "SJS000001";
	            }
	            else
	            {
	                return "SJS" + string.Format("{0:D6}", int.Parse(maxId.Substring(3)) + 1);
	            }
	        }
	    }
	    
	  

	    public List<CustomerFirstClass> GetSysCustomerFirstClass()
	    {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.CustomerFirstClass>(KVEntity.KEY_CUSTOMER_FIRST_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
        }

	    public void AddCustomerFirstClass(CustomerFirstClass item)
	    {
			var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.CustomerFirstClass>(KVEntity.KEY_CUSTOMER_FIRST_CLASS);
			var maxId = 1;
			if(items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
			item.Id = maxId;
			item.CreateTime = item.LastModifyTime = new DateTime ();
		    if (items == null) return;
		    items.Add (item);
            SetAllKvItemClass(items, KVEntity.KEY_CUSTOMER_FIRST_CLASS);
	    }

	    public void UpdateCustomerFirstClass(CustomerFirstClass item)
	    {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.CustomerFirstClass>(KVEntity.KEY_CUSTOMER_FIRST_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    break;
                }
                SetAllKvItemClass<SharpErp.Data.Common.Entity.CustomerFirstClass>(items, KVEntity.KEY_CUSTOMER_FIRST_CLASS);
            }
        }

        public void DeleteCustomerFirstClass(int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.CustomerFirstClass>(KVEntity.KEY_CUSTOMER_FIRST_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<SharpErp.Data.Common.Entity.CustomerFirstClass>(items, KVEntity.KEY_CUSTOMER_FIRST_CLASS);
            }
        }
        public void AddCustomerGoodsClass(CustomerGoodsClass item)
        {

            using (var context = new Common.Context.SqliteDbContext())
            {
                context.Add(context.CustomerGoodsClasses, item);
            }
        }

        public void DeleteCustomerGoodsClass(int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                context.Delete(context.CustomerGoodsClasses, p => p.Id == id);
            }
        }

        public void DeleteCustomerGoodsClassByCustomerAndItem(int customerSecondId, int itemNineClassId)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                context.Delete(context.CustomerGoodsClasses, p => p.CustomerSecondId == customerSecondId && p.NineClassId == itemNineClassId);
            }

        }

        public List<CustomerGoodsClass> GetCustomerGoods(int customerFirstId, int customerSecondId)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                return new System.Collections.Generic.List<CustomerGoodsClass>(context.SelectAll(context.CustomerGoodsClasses, p => p.CustomerSecondId == customerSecondId && p.CustomerSecondId == customerSecondId &&
                (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
            }
        }

         public List<QueryGoodsInfoView> GetCustomerGoodsView(int customerFirstId, int customerSecondId)
        {
            var result = new List<QueryGoodsInfoView>();
            var list = GetCustomerGoods(customerFirstId, customerSecondId);
            if(list != null && list.Count > 0)
            {
                foreach(var item in list)
                {
                    if (customerFirstId < 3) result.Add(getQueryGoodsInfoView(item.NineClassId));
                    else result.Add(getQueryMaterialInfoView(item.NineClassId));

                }
            }
            return result;
        }
    }
}
